data "alicloud_vswitches" "this" {
  ids    = [var.vswitch_id]
  status = "Available"
}


resource "alicloud_instance" "this" {
  for_each = var.instances  # 遍历实例配置映射
  
  instance_name        = each.value.name
  host_name            = each.value.host_name
  password             = var.password
  instance_type        = each.value.instance_type  # 支持不同实例类型
  image_id             = var.image_id
  system_disk_category = "cloud_essd"
  key_name             = var.key_name 
  system_disk_size     = 100
  security_groups      = var.security_group_ids
  vswitch_id           = var.vswitch_id
  internet_max_bandwidth_out = var.internet_max_bandwidth_out
  
  # 添加付费类型配置
  instance_charge_type = var.pay_type
  
  lifecycle {
    precondition {
      condition     = length(data.alicloud_vswitches.this.vswitches) > 0
      error_message = "Specified VSwitch ${var.vswitch_id} not found"
    }
  }
}